import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/manager/home' },
    {
      path: '/manager',
      component: () => import('@/views/ManagerLayout.vue'),
      children: [
        { path: 'home', meta: { name: '系统首页', requiresAuth: true }, component: () => import('@/views/manager/Home.vue'),  },
        { path: 'admin', meta: { name: '管理员信息', requiresAuth: true }, component: () => import('@/views/manager/Admin.vue'), },
        { path: 'notice', meta: { name: '系统公告', requiresAuth: true }, component: () => import('@/views/manager/Notice.vue'), },
        { path: 'new', meta: { name: '新闻资讯', requiresAuth: true }, component: () => import('@/views/manager/News.vue'), },
        { path: 'examPlan', meta: { name: '小测安排', requiresAuth: true }, component: () => import('@/views/manager/ExamPlan.vue'), },
        { path: 'questionType', meta: { name: '题型信息', requiresAuth: true }, component: () => import('@/views/manager/QuestionType.vue'), },
        { path: 'question', meta: { name: '题库信息', requiresAuth: true }, component: () => import('@/views/manager/Question.vue'), },
        { path: 'testPaper', meta: { name: '交流分享', requiresAuth: true }, component: () => import('@/views/manager/ExamPaperAD.vue'), },
        { path: 'person', meta: { name: '个人资料', requiresAuth: true }, component: () => import('@/views/manager/MineAD.vue'), },
        { path: 'password', meta: { name: '修改密码', requiresAuth: true }, component: () => import('@/views/manager/Password.vue'), },
        { path: 'teacher', meta: { name: '教师信息', requiresAuth: true }, component: () => import('@/views/manager/Teacher.vue'), },
        { path: 'user', meta: { name: '普通用户信息', requiresAuth: true }, component: () => import('@/views/manager/User.vue'), },
        { path: 'course', meta: { name: '课程信息', requiresAuth: true }, component: () => import('@/views/manager/Course.vue'), },
        { path: 'plantClass', meta: { name: '植物分类', requiresAuth: true }, component: () => import('@/views/manager/PlantClass.vue'), },
        { path: 'plantInfo', meta: { name: '植物信息', requiresAuth: true }, component: () => import('@/views/manager/PlantInfo.vue'), },
        { path: 'plantGuide', meta: { name: '植物指南', requiresAuth: true }, component: () => import('@/views/manager/PlantGuide.vue'), },
        { path: 'share', meta: { name: '交流分享', requiresAuth: true }, component: () => import('@/views/manager/ArticleShare.vue'), },
        { path: 'score', meta: { name: '成绩判断', requiresAuth: true }, component: () => import('@/views/manager/ExamScoreAD.vue'), },
        { path: 'plantCircle', meta: { name: '动态圈管理', requiresAuth: true }, component: () => import('@/views/manager/PlantCircle.vue'), },
        { path: 'sensitiveWords', meta: { name: '敏感词管理', requiresAuth: true }, component: () => import('@/views/manager/SensitiveWords.vue'), },
        { path: 'pointsGoods', meta: { name: '积分商品管理', requiresAuth: true }, component: () => import('@/views/manager/PointsGoods.vue'), },
        { path: 'pointsOrder', meta: { name: '积分兑换单管理', requiresAuth: true }, component: () => import('@/views/manager/PointsOrder.vue'), },
        { path: 'pointsTransaction', meta: { name: '积分流水管理', requiresAuth: true }, component: () => import('@/views/manager/PointsTransaction.vue'), },
        { path: 'pointsRule', meta: { name: '积分规则管理', requiresAuth: true }, component: () => import('@/views/manager/PointsRule.vue'), },
        { path: 'plantMoment', meta: { name: '动态管理', requiresAuth: true }, component: () => import('@/views/manager/PlantMoment.vue'), },

      ]
    },
    {
      path: '/front',
      component: () => import('@/views/FrontLayout.vue'),
      children: [
        { path: 'home', meta: { name: '首页', requiresAuth: true }, component: () => import('@/views/front/Home.vue'),  },
        { path: 'plant', meta: { name: '植物', requiresAuth: true }, component: () => import('@/views/front/Plant.vue'),  },
        { path: 'person', meta: { name: '个人资料', requiresAuth: true }, component: () => import('@/views/front/Mine.vue'),  },
        { path: 'exam', meta: { name: '考试', requiresAuth: true }, component: () => import('@/views/front/Exam.vue'),  },
        { path: 'testPaper', meta: { name: '试卷', requiresAuth: true }, component: () => import('@/views/front/ExamPaper.vue'),  },
        { path: 'score', meta: { name: '成绩', requiresAuth: true }, component: () => import('@/views/front/ExamScore.vue'),  },
        { path: 'testPaperView', meta: { name: '试卷详情', requiresAuth: true }, component: () => import('@/views/front/ExamPaperView.vue'),  },
        { path: 'myShare', meta: { name: '我的分享', requiresAuth: true }, component: () => import('@/views/front/MyArticle.vue'),  },
        { path: 'article', meta: { name: '论坛', requiresAuth: true }, component: () => import('@/views/front/Article.vue'),  },
        { path: 'articleDetail', meta: { name: '文章详情', requiresAuth: true }, component: () => import('@/views/front/ArticleDetail.vue'),  },
        { path: 'plantDetail', meta: { name: '植物详情', requiresAuth: true }, component: () => import('@/views/front/PlantDetail.vue'),  },
        { path: 'noticeDetail', meta: { name: '通知详情', requiresAuth: true }, component: () => import('@/views/front/NoticeDetail.vue'),  },
        { path: 'plantCircle', meta: { name: '植物圈', requiresAuth: true }, component: () => import('@/views/front/PlantCircle.vue'),  },
        { path: 'plantCircleDetail', meta: { name: '植物圈动态信息', requiresAuth: true }, component: () => import('@/views/front/PlantCircleDetail.vue'),  },
        { path: 'message', meta: { name: '消息通知', requiresAuth: true }, component: () => import('@/views/front/Message.vue'),  },
        { path: 'myCircle', meta: { name: '我的动态信息', requiresAuth: true }, component: () => import('@/views/front/MyCircle.vue'),  },
        { path: 'plantGuideDetail', meta: { name: '植物指南详情', requiresAuth: true }, component: () => import('@/views/front/PlantGuideDetail.vue'),  },
        { path: 'plantGuideList', meta: { name: '植物指南列表', requiresAuth: true }, component: () => import('@/views/front/PlantGuideList.vue'),  },
        { path: 'pointsShop', meta: { name: '敏感词管理', requiresAuth: true }, component: () => import('@/views/front/PointsShop.vue'), },
        { path: 'pointsOrderUser', meta: { name: '积分对单管理', requiresAuth: true }, component: () => import('@/views/front/PointsOrderUser.vue'), },
        { path: 'pointsTranUser', meta: { name: '积分流水', requiresAuth: true }, component: () => import('@/views/front/PointsTranUser.vue'), },
        { path: 'weather', meta: { name: '天气', requiresAuth: true }, component: () => import('@/views/front/Weather.vue'), },

      ]
    },
    { path: '/login', component: () => import('@/views/Login.vue') },
    { path: '/404', component: () => import('@/views/404.vue') },
    { path: '/:pathMatch(.*)', redirect: '/404' }
  ]
})
import { ref } from 'vue'
// 全局的 unreadCount
export const unreadCount = ref(0);
// 添加导航守卫
router.beforeEach((to, from, next) => {
  const user = JSON.parse(localStorage.getItem("user") || "{}");
  // 如果目标路由是 /front/message，重置 unreadCount
  if (to.path === '/front/message') {
    unreadCount.value = 0;  // 重置未读消息计数
  }
  // 如果用户是 USER 角色且尝试访问根路径，则重定向到 /front/home
  if (to.path === '/' && user.role === 'USER' && user.token) {
    return next('/front/home');
  }

  // 如果用户没有 token 且尝试访问需要认证的路径，则重定向到 /login
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!user.token) {
      return next('/login');
    }
  }

  // 如果用户是 USER 角色且尝试访问 /manager 路径，则重定向到 /front/home
  if (user.role === 'USER' && to.path.startsWith('/manager')) {
    return next('/front/home');
  }

  // 如果用户没有 token 且访问根路径，则重定向到 /login
  if (to.path === '/' && !user.token) {
    return next('/login');
  }

  // 其他情况继续导航
  next();
});

export default router



